home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / lbn.lqr / lbn.lbr / lbn.c next >
Text File  |  2011-02-01  |  7KB  |  314 lines

  1. /*
  2.  *    L B N . C
  3.  *
  4.  *
  5.  *    0.a.3        current version number
  6.  *    05jan84        date of last revision
  7.  *
  8.  *
  9.  *    This is a Line number and Braces depth Numbering utility for C programs.
  10.  *
  11.  *   Written by:
  12.  *
  13.  *    0.a.0    03sep82        Larry K. Blische
  14.  *
  15.  *
  16.  *   Modified by:
  17.  *
  18.  *    0.a.1    10sep82        Larry K. Blische
  19.  *                fix backslash mishandling
  20.  *
  21.  *      0.a.2   20jan83         Philip N. Hisley
  22.  *                              added user specified profile character
  23.  *
  24.  *    0.a.3    05jan84        Philip N. Hisley
  25.  *                modified to run under MSDOS via Lattice C
  26.  *                compiler
  27.  */
  28.  
  29. #include    <stdio.h>
  30.  
  31. #define    MAXCHAR            256
  32. #define    LINES_PER_PAGE        60
  33. #define    BAD            0
  34. #define    GOOD            1
  35. #define    FALSE            0
  36. #define    TRUE            1
  37. #define READ            "r"
  38. #define WRITE            "w"
  39. #define APPEND            "a"
  40. #define PROC            int
  41.  
  42. static    int    line_number    = 1;
  43. static    int    temp_line_num    = 0;
  44. static    int    page_number    = 1;
  45. static    int    temp_page_num    = 0;
  46.  
  47. static    int    braces_depth    = 0;
  48. static    int    neg_braces    = FALSE;
  49. static    int    paren_depth    = 0;
  50. static    int    neg_paren    = FALSE;
  51.  
  52. static    int    backslash    = FALSE;
  53. static    int    quote        = FALSE;
  54. static    int    double_quote    = FALSE;
  55. static    int    comment        = FALSE;
  56.  
  57. static  char    prfchr            = '_';    /* profile character */
  58. static    char    file_name[25];
  59. static  char    date_buf[25];
  60. static  char    prt_buf[MAXCHAR];
  61. static    char    line_buf[MAXCHAR];
  62. static    char    prefix_buf[17];
  63. static    char    time_buf[25];
  64.  
  65. PROC
  66. main( argc, argv )
  67. int    argc;
  68. char    *argv[];
  69. {
  70.     register int    n, acnt = 0;
  71.     FILE    *fio;
  72.  
  73.     /*
  74.      * see if command line has enough parameters
  75.      */
  76.  
  77.     if( argc < 2 ) {
  78.         printf("\nusage: lbn [ -c <char> ] fname.ext [ >fname.ext or device ]\n");
  79.         printf("\n         where: <char> is a user-specified\n");
  80.         printf("                profiling symbol ... the standard\n");
  81.         printf("                symbol is an underscore\n\n");
  82.         exit( BAD );
  83.     }
  84.  
  85.     /*
  86.      * get filename and time of day strings, try to open the file and then
  87.      *    put out the first header
  88.      */
  89.  
  90.     while( ++acnt < argc )
  91.         if( strcmp( argv[ acnt ], "-c" ) == 0 )
  92.             prfchr = *argv[ ++acnt ];
  93.         else
  94.             strcpy( &file_name, argv[ acnt ] );
  95.  
  96.     date( &date_buf, 7 );    /* Lattice library function */
  97.     time( &time_buf, 6 );   /* Lattice library function */
  98.  
  99.     time_buf[ sprintf( &time_buf, "%s     %s  ", &time_buf, &date_buf ) ] = NULL;
  100.  
  101.     if(( fio = fopen( &file_name, READ )) == NULL ) {
  102.         printf("\nlbn: *FATAL* can't open file %s\n", &file_name );
  103.         exit( BAD );
  104.     }
  105.     putheader();
  106.  
  107.     /*
  108.      * while there are lines left in the file, print them
  109.      */
  110.  
  111.     while( fgets( &line_buf, MAXCHAR, fio ) ) {
  112.         line_buf[ strlen( &line_buf ) ] = NULL;
  113.         set_prefix( &prefix_buf );
  114.         prt_buf[ 0 ] = NULL;
  115.         strcat( &prt_buf, &prefix_buf );
  116.         set_depth( &line_buf );
  117.         strcat( &prt_buf, &line_buf );
  118.         printf("%s", &prt_buf );
  119.         line_number++;
  120.         temp_line_num++;
  121.         if( temp_line_num == LINES_PER_PAGE ) {
  122.             temp_line_num = 0;
  123.             temp_page_num++;
  124.             putheader();
  125.         }
  126.     }
  127.     
  128.     /*
  129.      * close the file and print warnings
  130.      */
  131.     fclose( fio );
  132.     print_warnings();
  133. }
  134.  
  135. /*
  136.  *    P U T H E A D E R
  137.  *
  138.  * prints the top line of each page with time, filename, pagenumber
  139.  */
  140.  
  141. PROC
  142. putheader()
  143. {
  144.     char    page_buf[10];
  145.  
  146.     printf("\f\nLINE  DEPTH     SOURCE STATEMENTS");
  147.     printf("     %s  %s     PAGE  %d.%-d", &time_buf, &file_name,
  148.         page_number, temp_page_num );
  149.     printf("\n\n");
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156. /*
  157.  *    P R I N T _ W A R N I N G S
  158.  *
  159.  * prints braces, parens, quotes, and double quote mismatch warnings
  160.  */
  161.  
  162. PROC
  163. print_warnings()
  164. {
  165.     if( braces_depth != 0 ) {
  166.         printf("\nlbn: *WARNING* Braces mismatch." );
  167.         printf("%3d unmatched ", abs( braces_depth ) );
  168.         printf( (braces_depth < 0) ? "'}'" : "'{'" );
  169.         printf( (abs( braces_depth ) == 1) ? ".\n" : "s.\n" );
  170.     }
  171.     if( neg_braces )
  172.         printf("\nlbn: *WARNING* Braces depth went negative.\n");
  173.  
  174.     if( paren_depth != 0 ) {
  175.         printf("\nlbn: *WARNING* Parenthesis mismatch." );
  176.         printf("%3d unmatched ", abs( paren_depth ) );
  177.         printf( (paren_depth < 0) ? "')'" : "'('" );
  178.         printf( (abs( paren_depth ) == 1) ? ".\n" : "s.\n" );
  179.     }
  180.     if( neg_paren )
  181.         printf("\nlbn: *WARNING* Parenthesis depth went negative.\n");
  182.     if( quote )
  183.         printf("\nlbn: *WARNING* Single quote mismatch.\n" );
  184.  
  185.     if( double_quote )
  186.         printf("\nlbn: *WARNING* Double quote mismatch.\n" );
  187. }
  188.  
  189.  
  190. /*
  191.  *    S E T _ P R E F I X
  192.  *
  193.  * formats prefix string with correct braces depth
  194.  */
  195.  
  196. PROC
  197. set_prefix( pbuf )
  198. char    *pbuf;
  199. {
  200.     register int    i, j;
  201.     char        depth_buf[25];
  202.  
  203.     if( braces_depth < 0 ) {
  204.         depth_buf[0] = '?';
  205.         j = 1;
  206.     } else {
  207.         for( i = 1 ; i <= braces_depth ; i++ )
  208.             depth_buf[ i-1 ] = prfchr;
  209.         j = braces_depth;
  210.     }
  211.     for( i = j+1 ; i <= 10 ; i++ )
  212.         depth_buf[ i-1 ] = ' ';
  213.     depth_buf[10] = NULL;
  214.     sprintf( pbuf, "%4d  %s", line_number, &depth_buf );
  215. }
  216.  
  217. /*
  218.  *    S E T _ D E P T H
  219.  *
  220.  * determines braces depth, paren, quote, and double quote mismatch
  221.  */
  222.  
  223. PROC
  224. set_depth( pstr )
  225. char    *pstr;
  226. {
  227.     FOREVER {
  228.         switch( *pstr++ ) {
  229.         case '{':
  230.             if( backslash )
  231.                 backslash = FALSE;
  232.             else if( !quote && !double_quote && !comment )
  233.                 braces_depth++;
  234.             break;
  235.  
  236.         case '}':
  237.             if( backslash )
  238.                 backslash = FALSE;
  239.             else if( !quote && !double_quote && !comment )
  240.                 braces_depth--;
  241.             if( braces_depth < 0 )
  242.                 neg_braces = TRUE;
  243.             break;
  244.  
  245.         case '(':
  246.             if( backslash )
  247.                 backslash = FALSE;
  248.             else if( !quote && !double_quote && !comment )
  249.                 paren_depth++;
  250.             break;
  251.  
  252.         case ')':
  253.             if( backslash )
  254.                 backslash = FALSE;
  255.             else if( !quote && !double_quote && !comment )
  256.                 paren_depth--;
  257.             if( paren_depth < 0 )
  258.                 neg_paren = TRUE;
  259.             break;
  260.  
  261.         case '\n':
  262.             return;
  263.  
  264.         case '\\':
  265.             backslash = !backslash;
  266.             break;
  267.  
  268.         case '\'':
  269.             if( backslash )
  270.                 backslash = FALSE;
  271.             else if( !comment && !double_quote )
  272.                 quote = !quote;
  273.             break;
  274.  
  275.         case '\"':
  276.             if( backslash )
  277.                 backslash = FALSE;
  278.             else if( !comment && !quote )
  279.                 double_quote = !double_quote;
  280.             break;
  281.  
  282.         case '/':
  283.             if( backslash )
  284.                 backslash = FALSE;
  285.             else if( *pstr++ == '*' )
  286.                 comment = TRUE;
  287.             else
  288.                 pstr--;
  289.             break;
  290.  
  291.         case '*':
  292.             if( backslash )
  293.                 backslash = FALSE;
  294.             else if( *pstr++ == '/' )
  295.                 comment = FALSE;
  296.             else
  297.                 pstr--;
  298.             break;
  299.  
  300.         case '\f':
  301.             page_number++;
  302.             temp_page_num = 0;
  303.             temp_line_num = 0;
  304.             putheader();
  305.             *(pstr-1) = NULL;    /* putheader has the \f */
  306.             break;
  307.  
  308.         default:
  309.             backslash = FALSE;
  310.             break;
  311.         }
  312.     }
  313. }
  314.